package com.itic.system.region.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.system.region.persistence.dao.HppRegionDao;
import com.itic.system.region.persistence.model.HppRegion;
import com.itic.system.region.persistence.model.HppRegionExample;
import com.itic.system.region.persistence.model.HppRegionExample.Criteria;
import com.itic.system.vo.CarrageAreaVo;
import com.itic.system.vo.RegionCityVo;
import com.itic.system.vo.RegionPartVo;
import com.itic.system.vo.RegionProvinceVo;

@Service
@Transactional
public class HppRegionService extends CrudService<HppRegionDao, HppRegion, HppRegionExample> {
	@Autowired
	private HppRegionDao hppRegionDao;

	/**
	 * 根据区域ID获取区域名称
	 * 
	 * @param regionId
	 * @return
	 */
	public String getNameById(String regionId) {
		String name = null;
		if (StringHelper.isNotBlank(regionId)) {
			HppRegion region = this.get(regionId);
			if (region != null) {
				name = region.getRegionName();
			}
		}
		return name;
	}

	/**
	 * 获取所有省
	 * 
	 * @return
	 */
	public List<String> getProvince() {
		List<String> province = this.hppRegionDao.getProvince();
		return province;
	}

	/**
	 * 获取所有省列表
	 * 
	 * @return
	 */
	public List<RegionProvinceVo> getProvinces() {
		List<HppRegion> list = null;
		HppRegionExample example = new HppRegionExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andRegionLevelEqualTo(0);
		list = this.hppRegionDao.selectByExample(example);
		List<RegionProvinceVo> plist = new ArrayList<RegionProvinceVo>();
		for (HppRegion region : list) {
			RegionProvinceVo vo = new RegionProvinceVo();
			vo.setId(region.getId());
			vo.setRegionName(region.getRegionName());
			plist.add(vo);
		}
		return plist;
	}

	/**
	 * 获取省下市列表
	 * 
	 * @param provinceId
	 * @return
	 */
	public List<HppRegion> getCitys(String provinceId) {
		List<HppRegion> list = null;
		HppRegionExample example = new HppRegionExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andRegionLevelEqualTo(1);
		criteria.andParentRegionIdEqualTo(provinceId);
		list = this.hppRegionDao.selectByExample(example);
		return list;
	}

	/**
	 * 获取城市列表
	 * 
	 * @return
	 */
	public List<RegionCityVo> getcityList(String provinceId) {
		List<HppRegion> ctlist = this.getCitys(provinceId);
		List<RegionCityVo> citylist = new ArrayList<RegionCityVo>();
		for (HppRegion city : ctlist) {
			RegionCityVo cityvo = new RegionCityVo();
			cityvo.setCityId(city.getId());
			cityvo.setCityName(city.getRegionName());
			citylist.add(cityvo);
		}
		return citylist;
	}
	
	/**
	 * 根据市获取区
	 * @param cityId
	 * @return
	 */
	public List<HppRegion> getparts(String cityId){
		List<HppRegion> list = null;
		HppRegionExample example = new HppRegionExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andRegionLevelEqualTo(2);
		criteria.andParentRegionIdEqualTo(cityId);
		list = this.hppRegionDao.selectByExample(example);
		return list;
	}
	/**
	 * 根据城市获取区列表
	 * @param cityId
	 * @return
	 */
	public List<RegionPartVo> getpartList(String cityId){
		List<RegionPartVo> list = new ArrayList<RegionPartVo>();
		List<HppRegion> partlist = this.getparts(cityId);
		for(HppRegion part:partlist){
			RegionPartVo vo = new RegionPartVo();
			vo.setPartId(part.getId());
			vo.setPartName(part.getRegionName());
			list.add(vo);
		}
		return list;
	}
}